package com.wwj.app.v1;

import com.wwj.core.app.entity.AppAdvice;
import com.wwj.core.app.entity.AppBanner;
import com.wwj.core.app.entity.AppStartPage;
import com.wwj.core.app.entity.Version;
import com.wwj.core.app.service.IAppAdviceService;
import com.wwj.core.app.service.IAppBannerService;
import com.wwj.core.app.service.IAppStartPageService;
import com.wwj.core.app.service.IVersionService;
import com.wwj.core.wawa.entity.Banner;
import com.wwj.core.wawa.service.IBannerService;
import com.wwj.core.wawa.service.impl.BannerServiceImpl;
import com.wwj.framework.controller.BaseController;
import com.wwj.framework.utils.ReturnData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/3.
 */
@Controller
@RequestMapping("/v1/app")
public class AppController extends BaseController {

    @Resource
    private IAppStartPageService appStartPageService;

    @Resource
    private IAppBannerService appBannerService;

    @Resource
    private IBannerService bannerService;

    @Resource
    private IVersionService versionService;

    @Resource
    private IAppAdviceService appAdviceService;

    @RequestMapping("/startPages")
    @ResponseBody
    public ReturnData listStartPage() {
        ReturnData returnData = new ReturnData();
        try {
            List<AppStartPage> startPages = appStartPageService.findAppStartPages();
            returnData.success(startPages);
        } catch (Exception e) {
            logger.error("", e);
            returnData.failure("系统错误,请联系客服");
        }
        return returnData;
    }


    @RequestMapping("/banners")
    @ResponseBody
    public ReturnData listBanner() {
        ReturnData returnData = new ReturnData();
        try {
//            List<AppBanner> banners = appBannerService.findAppBanners();
//            if(banners != null && !banners.isEmpty()) {
//                Iterator<AppBanner> it = banners.iterator();
//                while (it.hasNext()) {
//                    AppBanner appBanner = it.next();
//                    if(appBanner.getStatus() == 0) {
//                        it.remove();
//                    }
//                }
//            }
            Map<String,Object> params = new HashMap<String,Object>();
            params.put("limit",0);
            params.put("offset",9999);
            List<Banner> banners = bannerService.findByPage(params, 0, 9999);
            if(banners != null && !banners.isEmpty()) {
                Iterator<Banner> it = banners.iterator();
                while (it.hasNext()) {
                    Banner appBanner = it.next();
                    if(appBanner.getStatus() == 0) {
                        it.remove();
                    }
                }
            }
            returnData.success(banners);
        } catch (Exception e) {
            logger.error("", e);
            returnData.failure("系统错误,请联系客服");
        }
        return returnData;
    }

    @RequestMapping("/advertisement")
    @ResponseBody
    public ReturnData advertisementPage() {
        ReturnData returnData = new ReturnData();
        try {
            List<AppStartPage> startPages = appStartPageService.findAppStartPages();
            AppStartPage first = null;
            for(AppStartPage startPage : startPages) {
                if(startPage.getStatus() == 1) {
                    first = startPage;
                    break;
                }
            }
            returnData.success(first);
        } catch (Exception e) {
            logger.error("", e);
            returnData.failure("系统错误,请联系客服");
        }
        return returnData;
    }

    @RequestMapping("/version")
    @ResponseBody
    public ReturnData version(String version, String os) {
        ReturnData returnData = new ReturnData();
        try {
            Version ver = versionService.dealValidateVersionNo(version, os);
            if(ver.getStatus() == 0) {
                returnData.failure("当前版本不可以用，请更新版本!");
            } else {
                returnData.success();
            }
        } catch (Exception e) {
            logger.error("", e);
            returnData.failure("系统错误,请联系客服");
        }
        return returnData;
    }

    @RequestMapping("/advice")
    @ResponseBody
    public ReturnData addAdvice(@RequestParam Integer playerId, @RequestParam String advice, @RequestParam String contact) {
        ReturnData returnData = new ReturnData();
        try {
            AppAdvice appAdvice = new AppAdvice();
            appAdvice.setPlayerId(playerId);
            appAdvice.setAdvice(advice);
            appAdvice.setContact(contact);
            appAdviceService.addAppAdvice(appAdvice);
            returnData.success();
        } catch (Exception e) {
            logger.error("", e);
            returnData.failure("系统错误,请联系客服");
        }
        return returnData;
    }
}
